define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pay/project/index',
                    add_url: 'pay/project/add',
                    edit_url: 'pay/project/edit',
                    del_url: 'pay/project/del',
                    multi_url: 'pay/project/multi',
                    table: 'pay_project',
                    add_apply_url: 'pay/apply/add',
                    index_apply_url: 'pay/apply/index'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                detailView: true,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        //{field: 'community_code', title: __('Community_code')},
                        //{field: 'code', title: __('Code')},
                        { field: 'name', title: __('Name') },
                        { field: 'startdate', title: __('StartDate'), formatter: Table.api.formatter.datetime, datetimeFormat: "YYYY-MM-DD" },
                        { field: 'endate', title: __('EndDate'), formatter: Table.api.formatter.datetime, datetimeFormat: "YYYY-MM-DD" },
                        { field: 'totalamount', title: __('TotalAmount') },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                // {

                                //     name: 'items',

                                //     title: '支出项',

                                //     icon: 'fa fa-cny',

                                //     classname: 'btn btn-success btn-xs btn-item'

                                // },
                                {
                                    name: 'apply',
                                    title: '结算申请',
                                    icon: 'fa fa-handshake-o',
                                    classname: 'btn btn-success btn-xs btn-apply'
                                },
                                {
                                    name: 'additem',
                                    title: '新增支出',
                                    icon: 'fa fa-cny',
                                    classname: 'btn btn-success btn-xs btn-additem'
                                }
                            ]

                        }
                    ]
                ],

                //注册加载子表的事件。注意下这里的三个参数！
                onExpandRow: function(index, row, $detail) {
                    initSubTable(index, row, $detail);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('click', '.btn-additem', function(e) {

                    e.stopPropagation();

                    var options = table.bootstrapTable('getOptions');

                    var index = $(this).closest('tr').data('index');

                    var data = table.bootstrapTable('getData');

                    var id = data[index][options.pk];



                    // Backend.api.addtabs('pay/index?addtabs=1&pid=' + id, '支出申请', 'fa fa-cny');
                    //  console.log(options.extend);

                    //Fast.api.open("pay/apply/add?dialog=1&pid=" + (id ? id : '0'), '新增支出');
                    Fast.api.open("pay/apply/addapply?pid=" + (id ? id : '0'), '新增支出');
                }),
                // table.on('click', '.btn-item', function(e) {

                //         e.stopPropagation();

                //         var options = table.bootstrapTable('getOptions');

                //         var index = $(this).closest('tr').data('index');

                //         var data = table.bootstrapTable('getData');

                //         var id = data[index][options.pk];



                //         Backend.api.addtabs('pay/index?addtabs=1&pid=' + id, '项目支出费用', 'fa fa-cny');



                //         //  console.log(options.extend);

                //         //Fast.api.open("expenses/index/add?dialog=1" + (id ? (options.extend.sublet_url.match(/(\?|&)+/) ? "&ids=" : "/ids/") + id : ''), __('PayItems'))

                //     }),
                table.on('click', '.btn-apply', function(e) {

                    e.stopPropagation();

                    var options = table.bootstrapTable('getOptions');

                    var index = $(this).closest('tr').data('index');

                    var data = table.bootstrapTable('getData');

                    var id = data[index][options.pk]

                    Backend.api.addtabs(options.extend.index_apply_url + (id ? "?pid=" + id : ''), '支出申请表', 'fa fa-handshake-o');
                    //Fast.api.open(options.extend.add_apply_url + (id ? "?pid=" + id : ''), '新增支出申请');

                })
        },
        add: function() {
            Controller.api.bindevent();
        },
        edit: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function() {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});


//初始化子表格(无线循环)
initSubTable = function(index, row, $detail) {
    var parentid = row.id;
    var cur_table = $detail.html('<table></table>').find('table');

    var html = "";
    html += "<table class='table table-bordered'>";
    html += "<thead>";
    html += "<tr style='height: 40px;text-algin:center'>";
    html += "<th>支出名称</th>";
    html += "<th>支出说明</th>";
    html += "<th>应付金额</th>";
    html += "<th>付款状态</th>";
    html += "</tr>";
    html += "</thead>";
    $.ajax({
        type: "get",
        url: "pay/index", //子表请求的地址
        data: { pid: row.id }, //我这里是点击父表后，传递父表列id和nama到后台查询子表数据
        async: false, //很重要，这里要使用同步请求
        success: function(data) {
            html += '<ul class="list-group" >';
            //遍历子表数据
            $.each(data.rows,
                function(n, value) {
                    html += "<tr  align='center'>" +
                        "<td>" + value.summary + "</td>" +
                        "<td>" + value.remark + "</td>" +
                        "<td>" + value.amount + "</td>";

                    if (value.status === 0)
                        html += "<td>未付</td>";
                    if (value.status === 1)
                        html += "<td>待审核</td>";
                    if (value.status === 2)
                        html += "<td>已付</td>";


                    html += "</tr>";
                });
            html += '</table>';
            $detail.html(html); // 关键地方
        }
    });

}